﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Manager : System.Web.UI.Page
{
    public void Clear()
    {
        txtTenSP.Text = "";
        txtDacTinh.Text = "";
        txtLoaiDongCo.Text = "";
        txtDungTich.Text = "";
        txtHTDanhLua.Text = "";
        txtLoaiKhung.Text = "";
        txtKTDR.Text = "";
        txtKTKhung.Text = "";
        txtDoCao.Text = "";
        txtThoiGian.Text = "";
        txtGiaTien.Text = "";
    }
    dbSanPham dbSP = new dbSanPham();
    public void LoadTTChung(int id)
    {
        XDocument loaded = XDocument.Load(Server.MapPath("~/App_Data/yamaha.xml"));
        var kq = from s in loaded.Descendants("LoaiSP")
                 where (int)s.Attribute("id") == id
                 select s;
        var sp = from c in kq.Descendants("yamaha")
                 select new
                 {
                     MaSP = (string)c.Element("MaSP"),
                     TenSP = (string)c.Element("TenSP"),
                     DacTinh = (string)c.Element("DacTinh"),
                     GiaTien = (string)c.Element("GiaTien"),
                     Hinh =  string.Concat("~/",(string)c.Element("Hinh"))
                 };
        gvHienThiTTChung.DataSource = sp;
        gvHienThiTTChung.DataBind();
    }
    public void LoadKT(int id)
    {
        XDocument loaded = XDocument.Load(Server.MapPath("~/App_Data/yamaha.xml"));
        var kq = from s in loaded.Descendants("LoaiSP")
                 where (int)s.Attribute("id") == id
                 select s;
        var sp = from d in kq.Descendants("yamaha")
                 select d;
        var cc = from c in sp.Descendants("KyThuat")
                 select new
                 {
                     MaSP = (string)c.Attribute("MaSP"),
                     LoaiDongCo = (string)c.Element("LoaiDongCo"),
                     DungTich = (string)c.Element("DungTich"),
                     HeThongDanhLua = (string)c.Element("HeThongDanhLua"),
                     LoaiKhung = (string)c.Element("LoaiKhung"),
                     KichThuoc = (string)c.Element("KichThuoc"),
                     KichThuocDaiRong = (string)c.Element("KichThuocDaiRong"),
                     DoCao = (string)c.Element("DoCao"),
                     ThoiGian = (string)c.Element("ThoiGian")
                 };
        gvHienThiKT.DataSource = cc;
        gvHienThiKT.DataBind();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        lblChao.Text = "Chào bạn <b><a href='#'>" + User.Identity.Name + "</a></b> đến với website của chúng tôi";
        if (!IsPostBack)
        {
            btnSua.Enabled = false;
            LoadTTChung(0);
            LoadKT(0);
        }
    }
    protected void ddlLoaiSP_SelectedIndexChanged(object sender, EventArgs e)
    {
        int id = ddlLoaiSP.SelectedIndex;
        LoadTTChung(id);
        LoadKT(id);
    }
    protected void btnThem_Click(object sender, EventArgs e)
    {
        int id = ddlLoaiSP.SelectedIndex;
        string ten = ddlLoaiSP.SelectedValue.ToString();
        string xml = Server.MapPath(@"App_Data/yamaha.xml");
        string TenSP = txtTenSP.Text;
        string DacTinh = txtDacTinh.Text;
        string LoaiDongCo = txtLoaiDongCo.Text;
        string DungTich = txtDungTich.Text;
        string HTDanhLua = txtHTDanhLua.Text;
        string LoaiKhung = txtLoaiKhung.Text;
        string KichThuoc = txtKTKhung.Text;
        string KichThuocDR=  txtKTDR.Text;
        string DoCao = txtDoCao.Text;
        string ThoiGianBH = txtThoiGian.Text;
        float GiaTien = float.Parse(txtGiaTien.Text);
        string Hinh = Server.MapPath(@"~/images/Hinh/"+ ten + "/" + fuHinh.FileName);
        fuHinh.SaveAs(Hinh);
        string HinhPath = @"images/Hinh/" + ten + "/" + fuHinh.FileName;
        SanPham sp = new SanPham
        {
            TenSanPham = TenSP,
            DacTinh = DacTinh,
            LoaiDongCo = LoaiDongCo,
            DungTich = DungTich,
            HTDanhLua = HTDanhLua,
            LoaiKhung = LoaiKhung,
            KichThuoc = KichThuoc,
            KichThuocDR = KichThuocDR,
            DoCao = DoCao,
            ThoiGianBH = ThoiGianBH,
            GiaTien = GiaTien,
            Hinh = HinhPath
        };
        dbSP.AddNewSanPham(sp, xml, id);
        LoadTTChung(id);
        LoadKT(id);
    }
    protected void btnSua_Click(object sender, EventArgs e)
    {
        int id = ddlLoaiSP.SelectedIndex;
        string ten = ddlLoaiSP.SelectedValue.ToString();
        int masp = int.Parse(txtAn.Text);
        string xml = Server.MapPath(@"App_Data/yamaha.xml");
        string TenSP = txtTenSP.Text;
        string DacTinh = txtDacTinh.Text;
        string LoaiDongCo = txtLoaiDongCo.Text;
        string DungTich = txtDungTich.Text;
        string HTDanhLua = txtHTDanhLua.Text;
        string LoaiKhung = txtLoaiKhung.Text;
        string KichThuoc = txtKTKhung.Text;
        string KichThuocDR = txtKTDR.Text;
        string DoCao = txtDoCao.Text;
        string ThoiGianBH = txtThoiGian.Text;
        float GiaTien = float.Parse(txtGiaTien.Text);
        string Hinh = Server.MapPath(@"~/images/Hinh/" + ten + "/" + fuHinh.FileName);
        fuHinh.SaveAs(Hinh);
        string HinhPath = @"images/Hinh/" + ten + "/" + fuHinh.FileName;
        SanPham sp = new SanPham
        {
            TenSanPham = TenSP,
            DacTinh = DacTinh,
            LoaiDongCo = LoaiDongCo,
            DungTich = DungTich,
            HTDanhLua = HTDanhLua,
            LoaiKhung = LoaiKhung,
            KichThuoc = KichThuoc,
            KichThuocDR = KichThuocDR,
            DoCao = DoCao,
            ThoiGianBH = ThoiGianBH,
            GiaTien = GiaTien,
            Hinh = HinhPath
        };
        dbSP.UpdateSanPham(sp, id, xml, masp);
        Clear();
        btnSua.Enabled = false;
        LoadTTChung(id);
        LoadKT(id);
    }
    protected void gvHienThiTTChung_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = ddlLoaiSP.SelectedIndex;
        string xml = Server.MapPath(@"App_Data/yamaha.xml");
        int MaSP = int.Parse(gvHienThiTTChung.DataKeys[e.RowIndex].Value.ToString());
        dbSP.DeleteSanPham(id, MaSP, xml);
        Response.Write("<SCRIPT LANGUAGE=''JavaScript''>alert('Chúc Mừng Bạn! Bạn Đã Xóa Thành Công')</SCRIPT>");
        LoadTTChung(id);
        LoadKT(id);
    }
    protected void gvHienThiTTChung_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        btnSua.Enabled = true;
        GridViewRow row = gvHienThiTTChung.Rows[e.NewSelectedIndex];
        GridViewRow row2 = gvHienThiKT.Rows[e.NewSelectedIndex];
        txtAn.Text = row.Cells[2].Text;
        txtTenSP.Text = row.Cells[3].Text;
        txtDacTinh.Text = row.Cells[4].Text;
        txtGiaTien.Text = row.Cells[5].Text;
        txtLoaiDongCo.Text = row2.Cells[3].Text;
        txtDungTich.Text = row2.Cells[4].Text;
        txtHTDanhLua.Text = row2.Cells[5].Text;
        txtLoaiKhung.Text = row2.Cells[6].Text;
        txtKTKhung.Text = row2.Cells[7].Text;
        txtKTDR.Text = row2.Cells[8].Text;
        txtDoCao.Text = row2.Cells[9].Text;
        txtThoiGian.Text = row2.Cells[10].Text;
       
    }

    protected void gvHienThiKT_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = ddlLoaiSP.SelectedIndex;
        string xml = Server.MapPath(@"App_Data/yamaha.xml");
        int MaSP = int.Parse(gvHienThiTTChung.DataKeys[e.RowIndex].Value.ToString());
        dbSP.DeleteSanPham(id, MaSP, xml);
        LoadTTChung(id);
        LoadKT(id);
    }
    protected void gvHienThiKT_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        btnSua.Enabled = true;
        GridViewRow row = gvHienThiTTChung.Rows[e.NewSelectedIndex];
        GridViewRow row2 = gvHienThiKT.Rows[e.NewSelectedIndex];
        txtAn.Text = row2.Cells[2].Text;
        txtLoaiDongCo.Text = row2.Cells[3].Text;
        txtDungTich.Text = row2.Cells[4].Text;
        txtHTDanhLua.Text = row2.Cells[5].Text;
        txtLoaiKhung.Text = row2.Cells[6].Text;
        txtKTKhung.Text = row2.Cells[7].Text;
        txtKTDR.Text = row2.Cells[8].Text;
        txtDoCao.Text = row2.Cells[9].Text;
        txtThoiGian.Text = row2.Cells[10].Text;
        txtTenSP.Text = row.Cells[3].Text;
        txtDacTinh.Text = row.Cells[4].Text;
        txtGiaTien.Text = row.Cells[5].Text;
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        Clear();
    }
    protected void lblDangXuat_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
        FormsAuthentication.RedirectToLoginPage();
    }
}